package cn.gudian.cms.GuestBook

class BackGuestController {

  def index = { redirect(action: "list", params: params) }

  // the delete, save and update actions only accept POST requests
  static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

  def list = {
    params.max = Math.min(params.max ? params.max.toInteger() : 10, 100)
    [backGuestInstanceList: BackGuest.list(params), backGuestInstanceTotal: BackGuest.count()]
  }

  def create = {
    def backGuestInstance = new BackGuest()
    backGuestInstance.properties = params
    return [backGuestInstance: backGuestInstance]
  }

  def save = {
    def backGuestInstance = new BackGuest(params)
    def url=params?.webUrl
    backGuestInstance.subTime = new Date();
    backGuestInstance.isSolve();
    if (!backGuestInstance.hasErrors() && backGuestInstance.save()) {
      flash.message = "反馈成功，古滇管理员会尽快和你联系，如果你的问题比较紧急，请在线咨询我们。QQ:938039269。"
      flash.args = [backGuestInstance.id]
      flash.defaultMessage = "BackGuest ${backGuestInstance.id} created"
      redirect(url:url)
    }
    else {
       flash.message = "反馈失败，请在线咨询我们。QQ:938039269。"
       redirect(url:url)
    }
  }

  def show = {
    def backGuestInstance = BackGuest.get(params.id)
    if (!backGuestInstance) {
      flash.message = "backGuest.not.found"
      flash.args = [params.id]
      flash.defaultMessage = "BackGuest not found with id ${params.id}"
      redirect(action: "list")
    }
    else {
      return [backGuestInstance: backGuestInstance]
    }
  }

  def edit = {
    def backGuestInstance = BackGuest.get(params.id)
    if (!backGuestInstance) {
      flash.message = "backGuest.not.found"
      flash.args = [params.id]
      flash.defaultMessage = "BackGuest not found with id ${params.id}"
      redirect(action: "list")
    }
    else {
      return [backGuestInstance: backGuestInstance]
    }
  }

  def update = {
    def backGuestInstance = BackGuest.get(params.id)
    if (backGuestInstance) {
      if (params.version) {
        def version = params.version.toLong()
        if (backGuestInstance.version > version) {

          backGuestInstance.errors.rejectValue("version", "backGuest.optimistic.locking.failure", "Another user has updated this BackGuest while you were editing")
          render(view: "edit", model: [backGuestInstance: backGuestInstance])
          return
        }
      }
      backGuestInstance.properties = params
      if (!backGuestInstance.hasErrors() && backGuestInstance.save()) {
        flash.message = "backGuest.updated"
        flash.args = [params.id]
        flash.defaultMessage = "BackGuest ${params.id} updated"
        redirect(action: "show", id: backGuestInstance.id)
      }
      else {
        render(view: "edit", model: [backGuestInstance: backGuestInstance])
      }
    }
    else {
      flash.message = "backGuest.not.found"
      flash.args = [params.id]
      flash.defaultMessage = "BackGuest not found with id ${params.id}"
      redirect(action: "edit", id: params.id)
    }
  }

  def delete = {
    def backGuestInstance = BackGuest.get(params.id)
    if (backGuestInstance) {
      try {
        backGuestInstance.delete()
        flash.message = "backGuest.deleted"
        flash.args = [params.id]
        flash.defaultMessage = "BackGuest ${params.id} deleted"
        redirect(action: "list")
      }
      catch (org.springframework.dao.DataIntegrityViolationException e) {
        flash.message = "backGuest.not.deleted"
        flash.args = [params.id]
        flash.defaultMessage = "BackGuest ${params.id} could not be deleted"
        redirect(action: "show", id: params.id)
      }
    }
    else {
      flash.message = "backGuest.not.found"
      flash.args = [params.id]
      flash.defaultMessage = "BackGuest not found with id ${params.id}"
      redirect(action: "list")
    }
  }

  def deleteAll = {
    def qstr = params.qstr
    if (qstr) {
      try {
        BackGuest.executeUpdate("delete BackGuest where id in(" + qstr + ")")
        flash.message = "delete successfully:" + qstr
        redirect(action: "list")
      } catch (Exception e) {
        flash.message = "delete failed:" + qstr
        redirect(action: "list")
      }
    }
  }
  def audit={
    def qstr = params.qstr
    if (qstr) {
      try {
        GuestBook.executeUpdate("update BackGuest set solve=true where id in(" + qstr + ")")
        flash.message = "已经解决成功:" + qstr
        redirect(action: "list")
      } catch (Exception e) {
        flash.message = "解决失败:" + qstr
        redirect(action: "list")
      }
    }
  }
}
